package com.njg.juc.interactive;


import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


// V1 synchronized + wait + notifyAll 传统老版本
/*class AirCondition{
    private int number = 0;

    public synchronized void increment () throws InterruptedException {
        //1.判断
        while (number != 0){
            this.wait();
        }
        //2.干活
        System.out.println(Thread.currentThread().getName() + "\t" + (++number));
        //3.通知
        this.notifyAll();
    }

    public synchronized void decrement () throws InterruptedException {
        //1,判断
        while (number == 0){
            this.wait();
        }
        //2.干活
        System.out.println(Thread.currentThread().getName() + "\t" + (--number));
        //3.通知
        this.notifyAll();
    }
}*/


//V2 JUC新版，Lock + condition.await() + condition.signalAll()
class AirCondition{
    private int number = 0;
    private final Lock lock = new ReentrantLock();
    private final Condition condition = lock.newCondition();

    public void increment() throws InterruptedException {
          lock.lock();
            try
            {
             while (number != 0){
                condition.await();
             }
             System.out.println(Thread.currentThread().getName() + "\t" + (++number));

             condition.signalAll();
            } finally {
                lock.unlock();
            }
    }

    public void decrement() throws InterruptedException {
          lock.lock();
            try
            {
             while (number == 0){
                 condition.await();
             }
             System.out.println(Thread.currentThread().getName() + "\t" + (--number));
            condition.signalAll();
            }  finally {
                lock.unlock();
            }
    }
}

/**
 * @auther zzyybs@126.com
 * @Date 2025-04-22 8:19
 * @Description: 有一个初始值为零的变量，现有两个线程对该变量操作，
 * 实现一个线程对变量加1，实现一个线程对变量减1,交替来10轮
 * <p>
 * *  笔记：Java里面如何进行工程级别的多线程编写
 * 1 多线程变成模板（套路）
 * 高内聚低耦合前提下，线程    操作    资源类
 * <p>
 * 2 多线程交互问题时， 判断    干活    通知
 * <p>
 * 3 需要注意防止虚假唤醒，wait方法需要用到while
 * <p>
 * 4
 */
public class ProdConsumerDemo {
    public static void main(String[] args) {
        AirCondition airCondition = new AirCondition();

        new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    try {
                        TimeUnit.MILLISECONDS.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //业务逻辑
                    airCondition.increment();
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "A").start();

        new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    try {
                        TimeUnit.MILLISECONDS.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //业务逻辑
                    airCondition.decrement();
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "B").start();

        new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    try {
                        TimeUnit.MILLISECONDS.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //业务逻辑
                    airCondition.increment();
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "C").start();

        new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    try {
                        TimeUnit.MILLISECONDS.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //业务逻辑
                    airCondition.decrement();
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "D").start();
    }

}
